// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

package probe

import (
	"context"
	"net/http"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"google.golang.org/protobuf/types/known/timestamppb"

	"github.com/iotexproject/iotex-core/v2/blockchain/block"
	"github.com/iotexproject/iotex-core/v2/testutil"
	"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

type testCase struct {
	endpoint string
	code     int
}

func testFunc(t *testing.T, ts []testCase) {
	for _, tt := range ts {
		resp, err := http.Get("http://localhost:7788" + tt.endpoint)
		require.NoError(t, err)
		assert.Equal(t, tt.code, resp.StatusCode)
	}
}

func TestBasicProbe(t *testing.T) {
	s := New(7788)
	ctx := context.Background()
	require.NoError(t, s.Start(ctx))
	require.NoError(t, testutil.WaitUntil(100*time.Millisecond, 2*time.Second, func() (b bool, e error) {
		_, err := http.Get("http://localhost:7788/liveness")
		return err == nil, nil
	}))
	test1 := []testCase{
		{
			endpoint: "/liveness",
			code:     http.StatusOK,
		},
		{
			endpoint: "/readiness",
			code:     http.StatusServiceUnavailable,
		},
		{
			endpoint: "/health",
			code:     http.StatusServiceUnavailable,
		},
	}
	testFunc(t, test1)

	test2 := []testCase{
		{
			endpoint: "/liveness",
			code:     http.StatusOK,
		},
		{
			endpoint: "/readiness",
			code:     http.StatusOK,
		},
		{
			endpoint: "/health",
			code:     http.StatusOK,
		},
	}
	require.NoError(t, s.TurnOn())
	testFunc(t, test2)
	require.NoError(t, s.TurnOff())
	testFunc(t, test1)

	now := time.Now()
	createFooter := func(ts time.Time) block.Footer {
		footer := block.Footer{}
		require.NoError(t, footer.ConvertFromBlockFooterPb(
			&iotextypes.BlockFooter{
				Timestamp: timestamppb.New(ts),
			},
		))
		return footer
	}

	require.NoError(t, s.ReceiveBlock(&block.Block{Footer: createFooter(now.Add(-9 * time.Second))}))
	require.True(t, s.IsReady())
	require.NoError(t, s.ReceiveBlock(&block.Block{Footer: createFooter(now.Add(-11 * time.Second))}))
	require.False(t, s.IsReady())

	require.NoError(t, s.Stop(ctx))
	_, err := http.Get("http://localhost:7788/liveness")
	require.Error(t, err)
}

func TestReadniessHandler(t *testing.T) {
	ctx := context.Background()
	s := New(7788, WithReadinessHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusAccepted)
	})))
	defer s.Stop(ctx)

	require.NoError(t, s.Start(ctx))
	require.NoError(t, testutil.WaitUntil(100*time.Millisecond, 2*time.Second, func() (b bool, e error) {
		_, err := http.Get("http://localhost:7788/liveness")
		return err == nil, nil
	}))
	test := []testCase{
		{
			endpoint: "/liveness",
			code:     http.StatusOK,
		},
		{
			endpoint: "/readiness",
			code:     http.StatusAccepted,
		},
		{
			endpoint: "/health",
			code:     http.StatusAccepted,
		},
	}
	require.NoError(t, s.TurnOn())
	testFunc(t, test)
}
